/**
 * Created by 小虎Oni on 2016/9/29.
 * update on 2016/10/9
 */
var AppDispatcher = require('./AppDisaptcher');
var EventEmitter = require('events').EventEmitter;
var assign = require('object-assign');
var config = require('../config');
var self = this;
var isDebug = config.isDebug;
function setData(pramsName,receivedTest){
    isDebug &&console.log("[OniStore] serData:"+pramsName);
    self[pramsName] = receivedTest
}
function emitChange(type){
    isDebug &&console.log("[OniStore] emitChange:"+type);
    OniStore.emit(type);
}
var OniStore = assign({},EventEmitter.prototype,{
    addChangeListener: function (type,callback) {
        this.on(type,callback)
    },
    removeChangeListener: function (type,callback) {
        this.removeListener(type,callback);
    },
    getData: function (pramsName) {
        isDebug &&console.log("[OniStore] getData:"+pramsName);
        return self[pramsName]||null;
    }
});
//默认执行设置数据，触发响应事件,如果有响应函数，执行回调,是自动调用的事件就执行下面的。
function handleAction(action){
    console.log("-----------------OniStore handleAction")
    if(action.isOni){
        isDebug &&console.log("[OniStore] handleAction:"+action.type);
        setData(action.type,action.data);
        action.callback && action.callback();
        emitChange(action.type);
    }
    //if(action.type === 'FIRST_TEST_ACTION'){
    //    setTest(action.testData);
    //    emitChange();
    //}
}
OniStore.dispatchToken = AppDispatcher.register(handleAction);
module.exports = OniStore;